### Replication Material ###
### The Manifesto Corpus ### 
### Wordcloud - Figure 1 ###

library(manifestoR)
library(wordcloud)

# set api key
mp_setapikey(key.file = "manifesto_apikey.txt")

# specify corpus version
mp_use_corpus_version("2016-1")

# load required manifestos
corp <- mp_corpus(
  (party==22320 | party == 22521 | party == 22722 | party == 22420) & 
  date == 201209, codefilter = c(607,608))

# preprocess text
corp <- tm_map(corp,removePunctuation)
corp <- tm_map(corp, content_transformer(tolower))
corp <- tm_map(corp,removeNumbers)
corp <- tm_map(corp, stripWhitespace)
corp <- tm_map(corp, removeWords, c(stopwords(kind="nl"), "vvd", "cda", "pvda", "pvv"))
corp <- tm_map(corp, stripWhitespace)

# generate term document matrizes
term.matrix <- TermDocumentMatrix(corp)
term.matrix.idf <- as.matrix(weightTfIdf(term.matrix, normalize = TRUE))
colnames(term.matrix.idf) <- c("PvdA","VVD","CDA","PVV")

# generate pdf
pdf(file="cloud.pdf", height=3, width=3)
comparison.cloud(term.matrix.idf,
                 scale=c(1.4,0.4), rot.per=0, max.words=600, random.order=FALSE,
                 colors=c("#FF0000","#1874CD","#228B22","#696969"))
dev.off()
